﻿namespace Toggl.PhoneClient.Data.SilverlightDb
{
    using ap2.Phone.Core.Data;
    using SilverlightPhoneDatabase;
    using Toggl.PhoneClient.Domain;

    public class SilverlightDbSessionFactory : ISessionFactory
    {
        private const string DatabaseName = "TogglDatabase";

        public SilverlightDbSessionFactory()
        {
            if (!Database.DoesDatabaseExists(DatabaseName))
            {
                Database database = Database.CreateDatabase(DatabaseName);
                database.CreateTable<Client>();
                database.CreateTable<Project>();
                database.CreateTable<PlannedTask>();
                database.CreateTable<Tag>();
                database.CreateTable<Task>();
                database.CreateTable<User>();
                database.CreateTable<Workspace>();
                database.Save();
            }
        }

        #region ISessionFactory Members

        public ISession Create()
        {
            return new SilverlightDbSession(Database.OpenDatabase(DatabaseName));
        }

        #endregion
    }
}
